import {
  createRouter,
  createWebHashHistory,
  type RouteLocationNormalized
} from "vue-router";
import VueLayout from "@/components/VueLayout/index.vue";
import HomeRoute from "@/views/Home/index.vue";
import NewsDetail from "@/views/NewsDetail/index.vue";
import { useCachedViewStoreHook } from "@/store/modules/cachedView";
import NProgress from "@/utils/progress";

const routes: any[] = [
  {
    path: "/",
    name: "BasicLayout",
    component: VueLayout,
    children: [
      {
        path: "/",
        name: "root",
        redirect: () => {
          return { path: "/home" };
        }
      },
      {
        path: "/home",
        name: "home-index",
        component: HomeRoute
      },
      {
        path: "/news/detail/:id",
        name: "news-detail",
        component: NewsDetail
      },
      {
        path: "video",
        name: "video-index",
        component: () => import("@/views/video/index.vue"),
        meta: {
          title: "video"
        }
      },
      {
        path: "policy",
        name: "policy",
        component: () => import("@/views/policy/index.vue"),
        meta: {
          title: "Privacy Policy"
        }
      },
      {
        path: "terms",
        name: "terms",
        component: () => import("@/views/terms/index.vue"),
        meta: {
          title: "Terms of Use"
        }
      }
    ]
  }
];
// 路由
const router = createRouter({
  history: createWebHashHistory(),
  routes
});
export interface toRouteType extends RouteLocationNormalized {
  meta: {
    title?: string;
    noCache?: boolean;
  };
}

router.beforeEach((to: toRouteType, _from, next) => {
  NProgress.start();
  // 路由缓存
  useCachedViewStoreHook().addCachedView(to);
  // 页面 title
  // set_app_title(to.meta.title);
  next();
});

router.afterEach(() => {
  NProgress.done();
});

// 导出
export default router;
